using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Voodoo.Meta;
using Voodoo.Pattern;
using Voodoo.Pattern.Random;

public class DuelModeManager : SingletonMB<DuelModeManager>
{
	public Transform DuelSpawnPosition;

	public Transform DuelCameraPosition;

	public DuelUpgradeLevels PlayerUpgrades;

	public DuelUpgradeLevels OpponentUpgrades;

	public List<DuelUpgrade> RVUpgrades;

	public List<DuelUpgrade> RareUpgrades;

	public List<DuelUpgrade> NormalUpgrades;

	public DuelCompleteUI DuelCompleteUI;

	public DuelPlayerNames PlayerNames;

	public UpgradeSelectionArea UpgradesContainer;

	public bool DuelStarted;

	public string OpponentSkin;

	public FlagPool _flags;

	public Sprite _opponentSprite;

	public Leaderboard _leaderboard;

	public Action OnDuelStarted;

	public string OpponentName;

	private float popupTimer = 10f;

	public Action OnUpgradedPlayer;

	public Action OnUpgradedOpponent;

	public float PlayerMoney;

	public float OpponentMoney;

	public string PlayersName
	{
		get
		{
			return NintendoPlayerPrefs.GetString("Player_Name", "Player");
		}
		set
		{
			NintendoPlayerPrefs.SetString("Player_Name", value);
		}
	}

	public void ChoseFlag()
	{
		_opponentSprite = _flags.flags.GetRandom();
	}

	public void SetUpCompetitors()
	{
		PlayerUpgrades = new DuelUpgradeLevels
		{
			Length = SingletonMB<Shop>.Instance.MaxLengthLevel / 2,
			Strength = SingletonMB<Shop>.Instance.MaxPowerLevel / 2,
			Size = SingletonMB<Shop>.Instance.MaxSizeLevel / 2,
			Multiplier = 1f,
			Treadmill = 0
		};
		OpponentUpgrades = new DuelUpgradeLevels
		{
			Length = SingletonMB<Shop>.Instance.MaxLengthLevel / 2,
			Strength = SingletonMB<Shop>.Instance.MaxPowerLevel / 2,
			Size = SingletonMB<Shop>.Instance.MaxSizeLevel / 2,
			Multiplier = 1f,
			Treadmill = 0
		};
		OnUpgradedPlayer?.Invoke();
		OnUpgradedOpponent?.Invoke();
	}

	public void SelectUpgrade(bool isPlayer, DuelUpgrade upgrade)
	{
		if (isPlayer)
		{
			if (!DuelStarted)
			{
				SingletonMB<GameManager>.Instance.StartGame();
			}
			switch (upgrade.UpgradeType)
			{
			case DuelUpgradeTypes.Length:
				PlayerUpgrades.Length += upgrade.Value;
				PlayerUpgrades.Length = Mathf.Clamp(PlayerUpgrades.Length, 1, SingletonMB<Shop>.Instance.MaxLengthLevel);
				break;
			case DuelUpgradeTypes.Size:
				PlayerUpgrades.Size += upgrade.Value;
				PlayerUpgrades.Size = Mathf.Clamp(PlayerUpgrades.Size, 1, SingletonMB<Shop>.Instance.MaxSizeLevel);
				break;
			case DuelUpgradeTypes.Strength:
				PlayerUpgrades.Strength += upgrade.Value;
				PlayerUpgrades.Strength = Mathf.Clamp(PlayerUpgrades.Strength, 1, SingletonMB<Shop>.Instance.MaxPowerLevel);
				break;
			case DuelUpgradeTypes.Multiplier:
				PlayerUpgrades.Multiplier += upgrade.Value;
				PlayerUpgrades.Multiplier = Mathf.Clamp(PlayerUpgrades.Multiplier, 1f, 4f);
				break;
			case DuelUpgradeTypes.Treadmill:
				PlayerUpgrades.Treadmill += upgrade.Value;
				break;
			}
			OnUpgradedPlayer?.Invoke();
			StartCoroutine(WaitAndEnablePlayer());
			UpgradesContainer.Hide();
		}
		else
		{
			switch (upgrade.UpgradeType)
			{
			case DuelUpgradeTypes.Length:
				OpponentUpgrades.Length += upgrade.Value;
				OpponentUpgrades.Length = Mathf.Clamp(OpponentUpgrades.Length, 1, SingletonMB<Shop>.Instance.MaxLengthLevel);
				break;
			case DuelUpgradeTypes.Size:
				OpponentUpgrades.Size += upgrade.Value;
				OpponentUpgrades.Size = Mathf.Clamp(OpponentUpgrades.Size, 1, SingletonMB<Shop>.Instance.MaxSizeLevel);
				break;
			case DuelUpgradeTypes.Strength:
				OpponentUpgrades.Strength += upgrade.Value;
				OpponentUpgrades.Strength = Mathf.Clamp(OpponentUpgrades.Strength, 1, SingletonMB<Shop>.Instance.MaxPowerLevel);
				break;
			case DuelUpgradeTypes.Multiplier:
				OpponentUpgrades.Multiplier += upgrade.Value;
				OpponentUpgrades.Multiplier = Mathf.Clamp(OpponentUpgrades.Multiplier, 1f, 4f);
				break;
			case DuelUpgradeTypes.Treadmill:
				OpponentUpgrades.Treadmill += upgrade.Value;
				break;
			}
			OnUpgradedOpponent?.Invoke();
			StartCoroutine(WaitAndEnableOpponent());
		}
	}

	private IEnumerator WaitAndEnableOpponent()
	{
		yield return null;
		SingletonMB<GameManager>.Instance.OpponentUpgraded();
	}

	internal void NameUpdated()
	{
		PlayerNames.Fill();
	}

	private IEnumerator WaitAndEnablePlayer()
	{
		yield return null;
		SingletonMB<GameManager>.Instance.PlayerUpgraded();
	}

	private void Update()
	{
		if (DuelStarted)
		{
			popupTimer -= Time.deltaTime;
			if (popupTimer < 0f)
			{
				popupTimer = 11f;
				StartCoroutine(ResetContainer());
			}
			UpgradesContainer.SetTimer(popupTimer);
		}
		if (Input.GetKeyDown(KeyCode.UpArrow))
		{
			SelectUpgrade(isPlayer: false, new DuelUpgrade
			{
				UpgradeType = DuelUpgradeTypes.Length,
				Value = 1
			});
		}
		if (Input.GetKeyDown(KeyCode.F))
		{
			SingletonMB<GameManager>.Instance.FinishLevel();
		}
	}

	public void SavePlayerCash(float value)
	{
		NintendoPlayerPrefs.SetFloat("Duel_Player_Cash", GetPlayerCash() + value);
	}

	public float GetPlayerCash()
	{
		return NintendoPlayerPrefs.GetFloat("Duel_Player_Cash", 0f);
	}

	public void ShowLeaderboardTheMain()
	{
		_leaderboard.Show((int)GetPlayerCash(), (int)(GetPlayerCash() + PlayerMoney), delegate
		{
			SingletonMB<GameManager>.Instance.LoadIntoMain();
		});
	}

	public void ShowLeaderboard(Action onComplete)
	{
		if (PlayerMoney > OpponentMoney)
		{
			_leaderboard.Show((int)GetPlayerCash(), (int)(GetPlayerCash() + PlayerMoney + OpponentMoney), onComplete);
			SavePlayerCash(PlayerMoney + OpponentMoney);
		}
		else
		{
			_leaderboard.Show((int)GetPlayerCash(), (int)(GetPlayerCash() + PlayerMoney), onComplete);
			SavePlayerCash(PlayerMoney + OpponentMoney);
		}
	}

	public void HideLeaderboard()
	{
		_leaderboard.gameObject.SetActive(value: false);
	}

	private IEnumerator ResetContainer()
	{
		UpgradesContainer.Hide();
		yield return new WaitForSeconds(0.5f);
		FillInNormalUpgrades();
		yield return new WaitForSeconds(0.5f);
		UpgradesContainer.Show();
	}

	private void Awake()
	{
		SetUpCompetitors();
		SingletonMB<GameManager>.Instance.IsDuelMode = true;
	}

	private void OnDestroy()
	{
		SingletonMB<GameManager>.Instance.IsDuelMode = false;
	}

	private void Start()
	{
		GameManager instance = SingletonMB<GameManager>.Instance;
		instance.OnGameStart = (Action)Delegate.Combine(instance.OnGameStart, new Action(GameStarted));
		FillInStartUpgrades();
	}

	private void FillInStartUpgrades()
	{
		if (50f.Choose())
		{
			UpgradesContainer.Fill(RVUpgrades.GetRandomRange(2), RareUpgrades.GetRandomRange(1), showReminder: false);
		}
		else
		{
			UpgradesContainer.Fill(RVUpgrades.GetRandomRange(1), RareUpgrades.GetRandomRange(2), showReminder: false);
		}
		UpgradesContainer.SetTimer(-1f);
	}

	public void OnGameFinished()
	{
		if (DuelStarted)
		{
			DuelStarted = false;
			float num = PlayerMoney + OpponentMoney;
			_ = 1000f;
			if (num < 2000f)
			{
				PlayerMoney *= 0.75f;
				OpponentMoney *= 0.75f;
			}
			if (num < 3000f)
			{
				PlayerMoney *= 0.5f;
				OpponentMoney *= 0.5f;
			}
			if (num < 4000f)
			{
				PlayerMoney *= 0.375f;
				OpponentMoney *= 0.375f;
			}
			if (num < 6000f)
			{
				PlayerMoney *= 0.25f;
				OpponentMoney *= 0.25f;
			}
			else
			{
				PlayerMoney *= 0.15f;
				OpponentMoney *= 0.15f;
			}
			DuelCompleteUI.Fill(PlayerMoney, OpponentMoney);
			DuelCompleteUI.Show();
		}
	}

	private void GameStarted()
	{
		DuelStarted = true;
		PlayerNames.gameObject.SetActive(value: false);
		OnDuelStarted?.Invoke();
	}

	public void FillInNormalUpgrades()
	{
		if (10f.Choose())
		{
			UpgradesContainer.Fill(RareUpgrades.GetRandomRange(2), NormalUpgrades.GetRandomRange(1), showReminder: true);
		}
		else if (50f.Choose())
		{
			UpgradesContainer.Fill(RareUpgrades.GetRandomRange(1), NormalUpgrades.GetRandomRange(2), showReminder: true);
		}
		else
		{
			UpgradesContainer.Fill(new List<DuelUpgrade>(), NormalUpgrades.GetRandomRange(3), showReminder: true);
		}
		SingletonMB<GameManager>.Instance.GameplayHaptic.PlayHaptic(HapticType.Big);
	}
}
